options compress=YES;
libname data ''; 

* APPEND SEGMENTS OF SERMBRPHUS ;

data work.append;
	set
		data.sermbrphus02 (in=seg02 keep = ssn doei phus_entry sexprimary ser_sex race ser_race dob ser_dob bdod ser_dod workyrs3060 sern: retire:)
		data.sermbrphus03 (in=seg03 keep = ssn doei phus_entry sexprimary ser_sex race ser_race dob ser_dob bdod ser_dod workyrs3060 sern: retire:)
		data.sermbrphus05 (in=seg05 keep = ssn doei phus_entry sexprimary ser_sex race ser_race dob ser_dob bdod ser_dod workyrs3060 sern: retire:)
		data.sermbrphus06 (in=seg06 keep = ssn doei phus_entry sexprimary ser_sex race ser_race dob ser_dob bdod ser_dod workyrs3060 sern: retire:)
		data.sermbrphus09 (in=seg09 keep = ssn doei phus_entry sexprimary ser_sex race ser_race dob ser_dob bdod ser_dod workyrs3060 sern: retire:)
		data.sermbrphus10 (in=seg10 keep = ssn doei phus_entry sexprimary ser_sex race ser_race dob ser_dob bdod ser_dod workyrs3060 sern: retire:)
		data.sermbrphus15 (in=seg15 keep = ssn doei phus_entry sexprimary ser_sex race ser_race dob ser_dob bdod ser_dod workyrs3060 sern: retire:)
		data.sermbrphus16 (in=seg16 keep = ssn doei phus_entry sexprimary ser_sex race ser_race dob ser_dob bdod ser_dod workyrs3060 sern: retire:)
		data.sermbrphus19 (in=seg19 keep = ssn doei phus_entry sexprimary ser_sex race ser_race dob ser_dob bdod ser_dod workyrs3060 sern: retire:)
		data.sermbrphus20 (in=seg20 keep = ssn doei phus_entry sexprimary ser_sex race ser_race dob ser_dob bdod ser_dod workyrs3060 sern: retire:)
	;

	if seg02 then segment = "02" ;
	if seg03 then segment = "03" ;
	if seg05 then segment = "05" ;
	if seg06 then segment = "06" ;
	if seg09 then segment = "09" ;
	if seg10 then segment = "10" ;
	if seg15 then segment = "15" ;
	if seg16 then segment = "16" ;
	if seg19 then segment = "19" ;
	if seg20 then segment = "20" ;
run ;

* Create variables ;

data data.sermbrphus_append;
	set work.append 
	(rename=(race=raceprimary dob=dobprimary bdod=bdodprimary));

	sex = sexprimary ;
	if sex = "" then sex = ser_sex ;

	race = raceprimary ;
	if race = "" then race = ser_race ;

	dob = dobprimary ;
	if dob = . then dob = ser_dob ;

	bdod = bdodprimary ;
	if bdod = . then bdod = ser_dod ;
	
	past_income = MEAN(OF sern50-sern59) ;
	life_income = SUM(OF sern55-sern68) ;

	* Sample: YOB between 1935 and 1947 ;
	if year(dob)>=1935 & year(dob)<=1947 & year(doei)<=year(dob)+70 & year(doei) ^= . ;
	dobyy = year(dob) ;

	format dob date9. ;
	format bdod date9. ;

	if 		past_income >=0 & 		past_income < 20000 then inccat = 1 ;
	else if	past_income >=20000 & 	past_income < 40000 then inccat = 2 ;
	else if past_income >=40000 & 	past_income < 60000 then inccat = 3 ;
	else if past_income >=60000 & 	past_income < 80000 then inccat = 4 ;
	else if past_income >=80000							then inccat = 5 ;

	keep ssn doei phus_entry sex race dob bdod segment retire1 retire2 past_income dobyy inccat sern55-sern68 life_income ;
 	
run ;

run; quit;

